<?php

class AppModel extends Model {

	var $cacheQueries = true;

	function findMy($type, $options = array()) {
		if ($this->hasField('user_id') && !empty($_SESSION['Auth']['User']['id'])) {
			$options['conditions'][$this->alias . '.user_id'] = $_SESSION['Auth']['User']['id'];
			return parent::find($type, $options);
		} else {
			return parent::find($type, $options);
		}
	}
	
	function saveMy($data, $params = array()) {
		if ($this->hasField('user_id')) {
			if (!empty($_SESSION['Auth']['User']['id'])) {
				$data[$this->alias]['user_id'] = $_SESSION['Auth']['User']['id'];
				parent::create();
				return parent::save($data, $params);
			} else {
				return false;
			}
		} else {
			parent::create();
			return parent::save($data, $params);
		}
	}
	
	function editMy($data, $params = array()) {
		if ($this->hasField('user_id') && !empty($_SESSION['Auth']['User']['id'])) {
			$options = array(
				'conditions' => array(
					$this->alias . '.user_id' => $_SESSION['Auth']['User']['id'],
					$this->alias . '.id' => $data[$this->alias]['id']
				)
			);
			if ($this->find('count', $options) > 0) {
				return parent::save($data, $params);
			} else {
				return false;
			}
		} else {
			return parent::save($data, $params);
		}
	}
	
	function deleteMy($id = null, $cascade = true) {
		if (!empty($id)) {
			$this->id = $id;
		}
		
		$id = $this->id;
		
		if ($this->hasField('user_id') && !empty($_SESSION['Auth']['User']['id'])) {
			$options = array(
				'conditions' => array(
					$this->alias . '.user_id' => $_SESSION['Auth']['User']['id'],
					$this->alias . '.id' => $id
				)
			);
			if ($this->find('count', $options) > 0) {
				return parent::delete($id, $cascade);
			} else {
				return false;
			}
		} else {
			return parent::delete($id, $cascade);
		}
	}
	
}

?>
